<template>
  <div id="create">
    <!-- 修改用户的对话框 -->
    <el-dialog title="修改用户" :visible.sync="dialog" :before-close="closeDialog" width="35%">
      <el-form ref="editFormRef" :rules="editFormReles" :model="editForm" label-position="right" label-width="auto" style="padding: 0 60px;">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name" />
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price" />
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight" />
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number" />
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">
          取消
        </el-button>
        <el-button type="primary" @click="confirmDialog">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
export default {
  name: 'EditItem',
  props: {
    // 组件接收父级传递的控制弹框 打开/关闭的状态值
    dialog: {
      type: Boolean,
      default: null
    },
    value: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      // 添加商品的表单数据对象
      editForm: {
        goods_name: '',
        goods_price: null,
        goods_weight: null,
        goods_number: null,
        // 商品所属的分类数组
        goods_cat: [],
        // 图片的数组
        pics: [],
        // 商品的详细描述
        goods_introduce: '请填写商品的详细描述',
        attrs: []
      },
      editFormReles: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        goods_cat: [
          { required: true, message: '请选择商品分类', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    // 点击取消
    closeDialog() {
      this.$emit('close')
    },
    // 点击确认，处理提交表单，向父级传递
    confirmDialog() {
      this.$refs['editFormRef'].validate((valid) => {
        if (valid) {
          // 定义一个变量为 editData 用来接收拷贝的 this.temp
          const editData = Object.assign({}, this.editForm)
          // console.log(editData)，打印看里面的字段
          this.$emit('edit', editData)
        }
      })
    }
  }
}
</script>
